<?php
namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;
use App\Http\Controllers\v1\PurviewController;

class CaseController extends Controller{
    /*
    案件列表(分类：所有案件、我参与案件)
    */
    public function CaseList($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $page = isset($pos['page']) ? $pos['page']  : 1;
        $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
        $offset = ($page - 1) * $pageSize;
        if(CheckController::CheckParameter($pos,['UserId','temp','key','caseType'])){//temp=完成与未完成mission_end为0表示未终止1表示终止
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            // print_r($UserPurview);die();
			$where=array();
            if($UserPurview == 'All'){//全部显示
                     if ($pos['temp'] == 0){//未完成案件
                        $where['mission_end'] = 0;
                    }
                    if($pos['temp'] == 1) {//完成案件
                        $where['mission_end'] = 1;
                    }
                    if($pos['caseType'] == 1){//诉讼项目
                        $where['case_type_id'] = 1;
                    }
                    if($pos['caseType'] == 2){//专项项目
                        $where['case_type_id'] = 2;
                    }
                    if($pos['caseType'] == 3){//法律顾问项目
                        $where['case_type_id'] = 3;
                    }
                    $CaseInfo = DB::connection('mysql_other')->table('yl_case as c')->select('c.approval_id as ApprovalId','m.mission_id as MissionId','c.case_num as CaseNum','c.case_id as CaseId','c.case_title as CaseTitle','c.case_create_time as CaseCreateTime','c.case_executor_id as CaseExecutorId','m.case_id as CaseId','m.mission_content as MissionContent','m.mission_count as MissionCount','m.mission_complete as MissionComplete','m.mission_end as MissionEnd','c.case_type_id as CaseTypeId')
                                                  
                                                    ->join("yl_mission as m",function($join){
                                                        $join->on('c.case_id','=','m.case_id');
                                                    })
                                                    ->where($where)
													//->where($map)
                                                    ->where('case_title','like','%'.$pos['key'].'%')
                                                    ->orderBy('c.case_id','desc');
                        $count =$CaseInfo->count();
                        $CaseInfo=$CaseInfo->offset($offset)->limit($pageSize)
                        ->get()->map(function ($val){
                            $val = (array)$val;
                            return $val;
                        })
                        ->toArray();
                        $sum_page = ceil($count/$pageSize);  //总页码
                        if($page > $sum_page && $sum_page > 0 ){
                            return RequestBackController::Structure(301);
                        }else{
							//print_r($CaseInfo);die;
                            if($CaseInfo){
                                $result['list'] = $this->ListData($CaseInfo); 
                                $result['count'] = $count;
                                return RequestBackController::Structure(501,$result);
                            }else{
                                return RequestBackController::Structure(504);
                            }
                                
                        }  

                }
            if($UserPurview == 'Personal'){//显示个人有关的案件
                
                if($pos['temp'] == 0){ //未完成
                    $where['mission_end'] = 0;
                }
                if($pos['temp'] == 1){//完成
                    $where['mission_end'] = 1;
                }
                if($pos['caseType'] == 1){//诉讼项目
                    $where['case_type_id'] = 1;
                }
                if($pos['caseType'] == 2){//专项项目
                    $where['case_type_id'] = 2;
                }
                if($pos['caseType'] == 3){//法律顾问项目
                    $where['case_type_id'] = 3;
                }
                $CaseInfo = DB::connection('mysql_other')->table('yl_case as c')->select('c.approval_id as ApprovalId','c.case_type_id as CaseTypeId','m.mission_id as MissionId','c.case_num as CaseNum','c.case_id as CaseId','c.case_title as CaseTitle','c.case_create_time as CaseCreateTime','c.case_executor_id as CaseExecutorId','m.case_id as CaseId','m.mission_content as MissionContent','m.mission_count as MissionCount','m.mission_complete as MissionComplete','m.mission_end as MissionEnd')
                                               ->join("yl_mission as m",function($join){
                                                    $join->on('c.case_id','=','m.case_id');
                                                })
                                                ->where($where)
                                                ->where('case_title','like','%'.$pos['key'].'%')
                                                // ->where('case_member_id','like','%'.$pos['UserId'].'%')->orWhere('case_from_id','like','%'.$pos['UserId'].'%')->orWhere('case_executor_id','like','%'.$pos['UserId'].'%')
                                                ->whereRaw("FIND_IN_SET($pos[UserId],case_member_id) or FIND_IN_SET($pos[UserId],case_from_id) or FIND_IN_SET($pos[UserId],case_executor_id)")
                                                ->orderBy('c.case_id','desc');
                    $count =$CaseInfo->count();
                    $CaseInfo=$CaseInfo->offset($offset)->limit($pageSize)
                    ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
                    $sum_page = ceil($count/$pageSize);  //总页码
                    if($page > $sum_page && $sum_page > 0 ){
                        return RequestBackController::Structure(301);
                    }else{
                        if($CaseInfo){
                            $result['list'] = $this->ListData($CaseInfo); 
                            $result['count'] = $count;
                            return RequestBackController::Structure(501,$result);
                        }else{
                            return RequestBackController::Structure(504);
                        }
                            
                    }
            }
            if($UserPurview == 'None'){//暂无任何权限
                return RequestBackController::Structure(403);
            }
 
        }else{
            return RequestBackController::Structure(502);
        }

    }
	public function CaseType($request)
	{
		if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId',])){
            return RequestBackController::Structure(502);
        }
		$res=DB::table('yl_case')->select('case_type_id as CaseTypeId','case_class_id as CaseClassId')->where('case_id',$pos['CaseId'])->first();
		return RequestBackController::Structure(101,$res);
	}
	

    /*
    案件管理列表
    */
    protected function ListData($info){
		//print_r($info);die;
            $UserInfo = DB::connection('mysql_other')->table('yl_user as u')->select('u.user_id as UserId','u.user_name as UserName','u.user_login as UserLogin','u.user_img as UserImg')
                ->get()->map(function ($val){
                    $val = (array)$val;
                    return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['UserId'];
                    })
                    ->toArray();	
			$case_id_arr=array();
			$ApprovalInfo = DB::connection('mysql_other')->table('yl_approval as a')->select('a.approval_id as ApprovalId','a.approval_is_end as ApprovalIsEnd')
                ->get()->map(function ($val){
                    $val = (array)$val;
                    return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['ApprovalId'];
                    })
                    ->toArray();
					//print_r($ApprovalInfo);die;
            foreach ($info as $k => $v) {
                $info[$k]['CaseExecutorId']=explode(',',$v['CaseExecutorId']);
				$case_id_arr[]=$v['CaseId'];
             
                foreach ($info[$k]['CaseExecutorId'] as $k1=>$v1) {
                    if(isset($UserInfo[$v1]))
                    $info[$k]['CaseExecutorId'][$k1] = $UserInfo[$v1];
                }
                $info[$k]['CaseCreateTime'] =date('Y-m-d H:s',$v['CaseCreateTime']);
                $info[$k]['ApprovalId'] = isset($ApprovalInfo[$info[$k]['ApprovalId']]['ApprovalIsEnd'])?$ApprovalInfo[$info[$k]['ApprovalId']]['ApprovalIsEnd']:'';
                if($info[$k]['ApprovalId'] == ''){
                   $info[$k]['Approvalstate'] ="待审批";
                }
                if($info[$k]['ApprovalId'] == 0){
                  $info[$k]['Approvalstate'] ="未通过";
                }
                if($info[$k]['ApprovalId'] == 1){
                   $info[$k]['Approvalstate'] ="待签署";
                }	
				if($info[$k]['ApprovalId'] == 2){
                   $info[$k]['Approvalstate'] ="已中止";
                }
            }		
			$yl_litigants=DB::table('yl_litigants')->whereIn('case_id',$case_id_arr)
					->get()
					->map(function ($val){
						$val = (array)$val;
						return $val;
						})
					->toArray();			
			$yl_visa_contract=DB::table('yl_visa_contract')->where('contract_file_path','<>','')->whereIn('case_id',$case_id_arr)
					->get()
					->map(function ($val){
						$val = (array)$val;
						return $val;
						})
					->keyBy(function($item)
                    {
                        return $item['case_id'];
                    })
					->toArray();
			$yl_visa_litigant=DB::table('yl_visa_litigant')->whereIn('case_id',$case_id_arr)
					->get()
					->map(function ($val){
						$val = (array)$val;
						return $val;
						})
					->toArray();		
			$record_data=DB::table('yl_record')->whereIn('case_id',$case_id_arr)
					->get()
					->map(function ($val){
						$val = (array)$val;
						return $val;
						})
						->keyBy(function($item)
						{
							return $item['case_id'];
						})
					->toArray();					
			$lit_temp=[];
			//print_r($yl_litigants);die();
			foreach($yl_litigants as $key=>$val)
			{
				$lit_temp[$val['case_id']]['litigants'][]=$val;
			}
			foreach($yl_visa_litigant as $key=>$val)
			{
				$lit_temp[$val['case_id']]['visa_litigants'][]=$val;
			}
			foreach($info as $k=>$v)
			{
				if(isset($yl_visa_contract[$v['CaseId']])&&!empty($yl_visa_contract[$v['CaseId']])&&isset($lit_temp[$v['CaseId']])&&isset($lit_temp[$val['case_id']]['visa_litigants'])&&count($lit_temp[$val['case_id']]['litigants'])==count($lit_temp[$val['case_id']]['visa_litigants']))
				{
					$info[$k]['Visa']=1;
				}
				else
				{
					$info[$k]['Visa']=0;
				}
				if(isset($record_data[$v['CaseId']]))
				{
					$info[$k]['RecordData']=1;
				}
				else
				{
					$info[$k]['RecordData']=0;
				}
			}
				
					
            if($info){
                return $info;
            }else{
                return false;
            }

    }
	
	
	
    /*
    案号
    */
    protected function Num(){
        $caseinfo = DB::connection('mysql_other')->table('yl_case')->select('case_id as CaseId')->orderBy('case_id','desc')->first();
        if($caseinfo){
            $num = $caseinfo->CaseId +1;
            return $num;
        }else{
           $num=1; 
           return $num;
        }

    }
	 /*
    编辑任务
    */  
     public function EditMission($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','MissionId','CaseActionDetailContent','MissionComplete'])){//
            return RequestBackController::Structure(502);
        }else{
			$row =DB::connection('mysql_other')->table('yl_mission')->where('mission_id',$pos['MissionId'])->first();
			if($row){
				if($pos['MissionComplete'] ==$row->mission_content || $pos['MissionComplete'] == $row->mission_complete ){
					$up = array();
					$up['mission_content'] = json_encode($pos['CaseActionDetailContent']);
					$up['mission_complete'] =$pos['MissionComplete'];//任务完成数
					$up['mission_count'] =0;//任务完成数
					foreach($pos['CaseActionDetailContent'] as $k=>$v)
					{
						$up['mission_count']+=count($v['task']);
					}					
					DB::connection('mysql_other')->table('yl_mission')->where('mission_id',$pos['MissionId'])->update($up);
					return RequestBackController::Structure(601);
				}
				$map = array();
				$map['mission_content'] = json_encode($pos['CaseActionDetailContent']);
				$map['mission_complete'] =$pos['MissionComplete'];//任务完成数
				$map['mission_count'] =0;//任务完成数
				foreach($pos['CaseActionDetailContent'] as $k=>$v)
				{
					$map['mission_count']+=count($v['task']);
				}
				DB::connection('mysql_other')->table('yl_mission')->where('mission_id',$pos['MissionId'])->update($map);
					return RequestBackController::Structure(601);
				
			}else{
				return RequestBackController::Structure(505);
			}
			
            
        }

    }
	/*
	获取单个mission任务
	*/
	public function ShowMissionById($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseId'])){//案件id
            return RequestBackController::Structure(502);
        }else{
		
            $TaskInfo = DB::connection('mysql_other')->table('yl_mission')->select('mission_id as MissionId','case_id as CaseId','mission_content as MissionContent','mission_count as MissionCount','mission_complete as MissionComplete','mission_end as MissionEnd')->where('case_id',$pos['CaseId'])->first();
            if($TaskInfo){
					$arr['MissionContent'] = json_decode($TaskInfo->MissionContent);
					$arr['MissionId'] =$TaskInfo->MissionId;
					$arr['CaseId'] =$TaskInfo->CaseId;
					$arr['MissionCount'] =$TaskInfo->MissionCount;
					$arr['MissionComplete'] =$TaskInfo->MissionComplete;
					$arr['MissionEnd'] =$TaskInfo->MissionEnd;
				
                return RequestBackController::Structure(501,$arr);
            }else{
                return RequestBackController::Structure(504);
            }
        }

    }
    //->whereRaw("FIND_IN_SET($menu_id,purview_personal)")
    /*
    利冲报告判定
    */
    public function Conflict($request){//将传过来的委托人和对方信息进行数据库查询。是否存在。转换角色查询
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['InterestConflict'])){//委托人数组人和对方当事人数组
            return RequestBackController::Structure(502);
        }else{
            $PrName=array();
            $PrCard=array();
            if($pos['InterestConflict']){//存在对方当事人
                foreach ($pos['InterestConflict'] as $k => $v){//将传过来的基本信息
                    if($v['retrieval'] == 1){//是否需要利冲查询
                        foreach($v['children'] as $k1 => $v1){
                            if(!isset($v1['IDCardCode'])){//不存在身份证号
                                $PrName[$k] = isset($v1['name'])?$v1['name']:'';//姓名
                            }else{
                                $PrCard[$k] = $v1['IDCardCode'];//身份证号
                            }
                            
                        }
                    }
                }
            }
            if($PrName){//姓名利冲
                $row=DB::connection('mysql_other')->table('yl_litigants as l')->select('l.litigant_id as LitigantId','l.case_id as CaseId',
                 'l.litigation_status_id as LitigationStatusId','l.litigant_name as LitigantName',
                 'l.litigant_id_card as LitigantIdCard','l.litigant_mobile as LitigantMobile','l.litigant_address as LitigantAddress',
                 'l.contact_person as ContactPerson','l.contact_person_mobile as ContactPersonMobile','l.litigant_id_card as LitigantIdCard',
                 'l.litigant_type_id as LitigantTypeId','ls.litigation_status_id as LitigationStatusId',
                 'ls.litigation_status_name as LitigationStatusName','l.litigant_able as LitigantAble','c.case_id as CaseId','c.case_title as CaseTitle','c.case_executor_id as CaseExecutorId','c.litigants_id as LitigantsId','c.case_create_time as CaseCreateTime')
                                    ->join("yl_litigation_status as ls",function($join){
                                                        $join->on('l.litigation_status_id','=','ls.litigation_status_id');
                                                    })
                                    ->join("yl_case as c",function($join){
                                        $join->on('c.case_id','=','l.case_id');
                                    })
                                    ->whereIn('l.litigant_name',$PrName)

                                    ->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                    })->toArray();
                                    //print_r($row);die();
            }
            if($PrCard){//身份证利冲
                $row=DB::connection('mysql_other')->table('yl_litigants as l')->select('l.litigant_id as LitigantId','l.case_id as CaseId',
                 'l.litigation_status_id as LitigationStatusId','l.litigant_name as LitigantName',
                 'l.litigant_id_card as LitigantIdCard','l.litigant_mobile as LitigantMobile','l.litigant_address as LitigantAddress',
                 'l.contact_person as ContactPerson','l.contact_person_mobile as ContactPersonMobile','l.litigant_id_card as LitigantIdCard',
                 'l.litigant_type_id as LitigantTypeId','ls.litigation_status_id as LitigationStatusId',
                 'ls.litigation_status_name as LitigationStatusName','l.litigant_able as LitigantAble','c.case_id as CaseId','c.case_title as CaseTitle','c.case_executor_id as CaseExecutorId','c.litigants_id as LitigantsId','c.case_create_time as CaseCreateTime')
                                    ->join("yl_litigation_status as ls",function($join){
                                                        $join->on('l.litigation_status_id','=','ls.litigation_status_id');
                                                    })
                                    ->join("yl_case as c",function($join){
                                        $join->on('c.case_id','=','l.case_id');
                                    })
                                    ->whereIn('l.litigant_id_card',$PrCard)

                                    ->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                    })->toArray();
                                    //print_r($row);die();
            }
            if($row){
                $temp=array();
                $t= array();
                foreach($row as $k => $v){
					if(isset($v['CaseCreateTime'] )){
							$row[$k]['CaseCreateTime'] =date('Y-m-d',$v['CaseCreateTime']);
						}
                    
                    if($v['LitigantAble'] == 1){
                        $temp[$k] = $v['LitigantId'];
                        $t[$k]['LitigantId'] =$v['LitigantId'];
                        $t[$k]['LitigationStatusName']=$v['LitigationStatusName'];
                        
                    }
                }
				//print_r($temp);die;
                $case= DB::connection('mysql_other')->table('yl_litigants as l')->select('c.case_id as CaseId','c.case_title as CaseTitle','c.case_executor_id as CaseExecutorId','c.litigants_id as LitigantsId','c.case_create_time as CaseCreateTime','l.litigant_id as LitigantId','l.case_id as CaseId','l.litigation_status_id as LitigationStatusId','s.litigation_status_id as LitigationStatusId','s.litigation_status_name as LitigationStatusName')
                                ->join("yl_case as c",function($join){
                                    $join->on('c.case_id','=','l.case_id');
                                })
                                ->join("yl_litigation_status as s",function($join){
                                    $join->on('l.litigation_status_id','=','s.litigation_status_id');
                                })
                                ->whereIn('c.litigants_id',$temp)
                                ->get()->map(function ($val){
                                $val = (array)$val;
                                return $val;
                                })
                                ->keyBy(function($item)
                                {
                                return $item['CaseId'];
                                })
                                ->toArray();
					//print_r($case);die;			
                $UserInfo = DB::connection('mysql_other')->table('yl_user as u')->select('u.user_id as UserId','u.user_name as UserName','u.user_login as UserLogin','u.user_img as UserImg')
                ->get()->map(function ($val){
                    $val = (array)$val;
                    return $val;
                    })
                    ->toArray();
                if($case){
                    foreach ($case as $k => $v) {
						if(isset($v['CaseCreateTime'] )){
							$case[$k]['CaseCreateTime'] =date('Y-m-d',$v['CaseCreateTime']);
						}
						
                        $case[$k]['CaseExecutorId']=explode(',',$v['CaseExecutorId']);
                        foreach ($case[$k]['CaseExecutorId'] as $k1=>$v1) {
							if(isset($UserInfo[$v1]))
                            $case[$k]['CaseExecutorId'][$k1] = $UserInfo[$v1];
                        }
						
                        
                    }
                    foreach ($t as $k => $v) {
                        $case[$k]['LitigantId'] = $v['LitigantId'];
                        $case[$k]['LitigantName'] = $v['LitigationStatusName'];
                    }
                    $data['case'] = $case;
                }
				//print_r($data);
                $data['personal'] = $row;
                return RequestBackController::Structure(501,$data);
            }else{
                return RequestBackController::Structure(504);
            }
         
       }


    }
	/*
    新建审批分类
    */
	public function AddApprovalType($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalTypeTitle'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $add=array();
                $add['approval_type_title'] = $pos['ApprovalTypeTitle'];
				$add['approval_type_created_time'] = time();
                $row = DB::connection('mysql_other')->table('yl_approval_type')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }
        }
    }
	/*审批分类显示*/
     public function ShowApprovalType($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $page = isset($pos['page']) ? $pos['page']  : 1;
        $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
        $offset = ($page - 1) * $pageSize;
        if(!CheckController::CheckParameter($pos,['UserId'])){
            return RequestBackController::Structure(502);
        }else{
            $row = DB::connection('mysql_other')->table('yl_approval_type')->select('approval_type_id as ApprovalTypeId','approval_type_title as ApprovalTypeTitle','approval_type_created_time as ApprovalTypeCreatedTime');
                $count =$row->count();
                $row=$row->offset($offset)->limit($pageSize)
                ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($page > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
					//print_r($row);die;
                    if(isset($row)){
						foreach ($row as $k => $v) {
                           $row[$k]['ApprovalTypeCreatedTime']=isset($v['ApprovalTypeCreatedTime'])?date('Y-m-d',$v['ApprovalTypeCreatedTime']):'';
                        }
						$rows['List'] =$row;
						$rows['Count'] =$count;
                        return RequestBackController::Structure(501,$rows);
                    }else{
                        return RequestBackController::Structure(504);
                    }
                           
                }
                
        }

    }
	  /*编辑审批分类*/
    public function EditApprovalType($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalTypeTitle','ApprovalTypeId'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $map=array();
                $map['approval_type_title'] = $pos['ApprovalTypeTitle'];
                $row = DB::connection('mysql_other')->table('yl_approval_type')->where('approval_type_id',$pos['ApprovalTypeId'])->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }else{
                return RequestBackController::Structure(403);
            }
        }
    }
	/*删除审批分类*/
    public function DeleteApprovalType($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalTypeId'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                if($pos['ApprovalTypeId'] > 0 ){
                    // print_r($pos['ApprovalTypeId']);die();
                    $approval = DB::connection('mysql_other')->table('yl_approval_mode')->where('approval_type_id',$pos['ApprovalTypeId'])->first();
                    if($approval){
                        return RequestBackController::Structure(604);//不能删除。模板中含有该分类
                    }else{
                        $row = DB::connection('mysql_other')->table('yl_approval_type')->where('approval_type_id',$pos['ApprovalTypeId'])->delete();
                        if($row){
                            return RequestBackController::Structure(601);
                        }else{
                            return RequestBackController::Structure(602);
                        }
                    }
                }else{
                    return RequestBackController::Structure(505);
                }
            }else{
                return RequestBackController::Structure(403);
            }


        }

    }
	  /*
        新建审批模板
    */
   public function AddApprovalMode($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalModeContent','ApprovalAssessorId','ApprovalCopyId','ApprovalTypeId','ApprovalModeTitle'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $add=array();
                $add['approval_mode_title'] = $pos['ApprovalModeTitle'];
                $add['approval_mode_content'] = $pos['ApprovalModeContent'];//模板内容
                $add['approval_assessor_id'] = $pos['ApprovalAssessorId'];//审批人合集
                $add['approval_copy_id'] = $pos['ApprovalCopyId'];//抄送人合集
                $add['approval_mode_creater'] = $pos['UserId'];
                $add['approval_mode_created_time'] = time();
                $add['approval_type_id'] = $pos['ApprovalTypeId'];//审批类型
                $add['approval_mode_updater'] = $pos['UserId'];
                $add['approval_mode_update_time'] = time();
                $row = DB::connection('mysql_other')->table('yl_approval_mode')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }


        }

    }
	 /*
        编辑审批模板
    */
    public function EditApprovalMode($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalModeId','ApprovalModeTitle','ApprovalModeContent','ApprovalAssessorId','ApprovalCopyId','ApprovalTypeId'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $map=array();
                $map['approval_mode_title'] = $pos['ApprovalModeTitle'];
                $map['approval_mode_content'] = $pos['ApprovalModeContent'];//模板内容
                $map['approval_assessor_id'] = $pos['ApprovalAssessorId'];//审批人合集
                $map['approval_copy_id'] = $pos['ApprovalCopyId'];//抄送人合集
                $map['approval_type_id'] = $pos['ApprovalTypeId'];//审批类型
                $map['approval_mode_updater'] = $pos['UserId'];
                $map['approval_mode_update_time'] = time();
                $row = DB::connection('mysql_other')->table('yl_approval_mode')->where('approval_mode_id',$pos['ApprovalModeId'])->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }


        }

    }
	 /*
        删除审批模板
    */
    public function DeleteApprovalMode($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalModeId'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){//管理员删除
                $rowIn = DB::connection('mysql_other')->table('yl_approval')->where('approval_mode_id',$pos['ApprovalModeId'])->first();
                if($rowIn){
                    return RequestBackController::Structure(605);//审批中在使用该模板不能删除
                }else{
                    $row = DB::connection('mysql_other')->table('yl_approval_mode')->where('approval_mode_id',$pos['ApprovalModeId'])->delete();
                    if($row){
                        return RequestBackController::Structure(601);
                    }else{
                        return RequestBackController::Structure(602);
                    }
                }

            }
            else{
                return RequestBackController::Structure(403);
            }


        }

    }
	/*查询审批模板*/
  public function ShowApprovalMode($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $page = isset($pos['page']) ? $pos['page']  : 1;
        $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
        $offset = ($page - 1) * $pageSize;
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalTypeId'])){
            return RequestBackController::Structure(502);
        }else{
            $row = DB::connection('mysql_other')->table('yl_approval_mode as m')->select('m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_mode_title as ApprovalModeTitle','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','m.approval_type_id as ApprovalTypeId','t.approval_type_id as ApprovalTypeId','t.approval_type_title as ApprovalTypeTitle','u.user_id as UserId','u.user_name as UserName')
                        ->join("yl_approval_type as t",function($join){
                            $join->on('m.approval_type_id','=','t.approval_type_id');
                        })->join("yl_user as u",function($join){
                            $join->on('u.user_id','=','m.approval_mode_creater');
                        })
                        ->where('m.approval_type_id',$pos['ApprovalTypeId']);
                        $count =$row->count();
                        $row=$row->offset($offset)->limit($pageSize)
                        ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
                    $sum_page = ceil($count/$pageSize);  //总页码
                    if($page > $sum_page && $sum_page > 0 ){
                        return RequestBackController::Structure(301);
                    }else{
                        if($row){
                            foreach ($row as $k => $v) {
                                $row[$k]['ApprovalModeCreatedTime'] =date('Y-m-d',$v['ApprovalModeCreatedTime']);
                                $row[$k]['ApprovalModeUpdateTime'] =date('Y-m-d',$v['ApprovalModeUpdateTime']);
                                $row[$k]['count']=$count;
                            }
                            return RequestBackController::Structure(501,$row);
                        }else{
                            return RequestBackController::Structure(504);
                        }
                           
                    }
             
        }

    }
	
	/*
    新建审批（登录可建）
    */
      public function AddApproval($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalModeId','CaseId','ApprovalContent'])){
            return RequestBackController::Structure(502);
        }else{
                $add=array();
                $add['approval_mode_id'] = $pos['ApprovalModeId'];//模板ID
                $add['case_id'] = $pos['CaseId'];//案件id
                $add['approval_content'] = $pos['ApprovalContent'];//审批主体
                $add['approval_creater_id'] = $pos['UserId'];//审批发起人
                $add['approval_created_time'] = time();//发起时间
                $row = DB::connection('mysql_other')->table('yl_approval')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

        }

    }
	/*
    显示审批处理结果
    */
     public function ShowApprovalResults($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId'])){
            return RequestBackController::Structure(502);
        }else{
            $row = DB::connection('mysql_other')->table('yl_approval_results')->select('approval_results_id as ApprovalResultsId','approval_results_title as ApprovalResultsTitle')
                        ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
            if($row){
                return RequestBackController::Structure(501,$row);
            }else{
                return RequestBackController::Structure(504);
            }
                
        }

    }

    /*
        案件类型列表和案件分类列表
    */
        public function CaseTypeList($request){
            if(!CheckController::CheckUser($request)){
                return RequestBackController::Structure(205);
            }
            $TypeList = DB::connection('mysql_other')->table('yl_case_type')->select('case_type_id as CaseTypeId','case_type_title as CaseTypeTitle')->get() ->toArray();
            $ClassList = DB::connection('mysql_other')->table('yl_case_class')->select('case_class_id as CaseClassId','case_class_title as CaseClassTitle')->get() ->toArray();
            if($TypeList || $ClassList ){
                $CaseType['TypeList'] = $TypeList;
                $CaseType['ClassList'] = $ClassList;
                return RequestBackController::Structure(501,$CaseType);
            }else{
                return RequestBackController::Structure(504);
            }
            
        }

    /*
    案件新增时的权限判定
    */
    public function CasePurview($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(CheckController::CheckParameter($pos,['UserId'])){
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All' || $UserPurview == 'Personal' ){//全部和个体允许添加
                //允许进行案件新增
                return true;

            }
            if($UserPurview == 'None'){
                return RequestBackController::Structure(403);
            }

        }

    }
	 /*
        案件来源列表
    */
    public function CaseSourceList($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $row = DB::connection('mysql_other')->table('yl_case_source')->select('case_source_id as CaseSourceId','case_source_title as CaseSourceTitle')
                ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })->toArray();
        if($row){
            return RequestBackController::Structure(501,$row);
        }else{
            return RequestBackController::Structure(504);
        }
    }
	 /*
        案件利益冲突列表
    */
    public function CaseConflictList($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $row = DB::connection('mysql_other')->table('yl_case_conflict_rule')->select('case_conflict_id as CaseConflictId','case_conflict_title as CaseConflictTitle')
                ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })->toArray();
        if($row){
            return RequestBackController::Structure(501,$row);
        }else{
            return RequestBackController::Structure(504);
        }
    }



    /*
        新建案件项目（需要将案件与任务模板mission_mode 确定关联再关联上mission任务。以至于可展示任务模板时直接将mission_content字段传给前端）
        CaseTypeId(案件类型) CaseClassId(案件分类)CaseActionId(案由id)case_subject_matter(诉讼标的)case_pay_count(收费数额)case_pay_way(收费方式)
        case_stage(审级或阶段)case_acceptance_id(受理机关id)litigants_id(当事人id集)litigation_status_id(诉讼地位id)litigation_status_name(诉讼地位名称)litigant_name(当事人姓名)litigant_mobile(当事人联系方式)case_situation(简要案情)case_from_id(项目案源律师id)case_executor_id(项目承办律师id)case_num(案号)
    */
    public function CaseAdd($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseTitle','CaseSubjectMatter','CasePayCount','CaseChargeId','CaseStage','CaseAcceptanceId','CaseSituation','CaseSource','CaseActionInfo','CaseLinkman','CaseLinkmanTel','CaseConflictId'])){//新建项目传来参数
            return RequestBackController::Structure(502);
        }else{
            //收费比列如何添加case_pay_ratio
            $map = array();
            $Litigant = array();
            $mis = array();
            //$map['case_title'] = $pos['CaseTitle'];//案件标题
            $map['case_type_id'] =isset($pos['CaseTypeId'])?$pos['CaseTypeId']:'';//案件类型
		    $map['case_action_info'] =isset($pos['CaseActionInfo'])?$pos['CaseActionInfo']:'';//案由简介
            $map['case_linkman'] =isset($pos['CaseLinkman'])?$pos['CaseLinkman']:'';//案件联系人
            $map['case_linkman_tel'] =isset($pos['CaseLinkmanTel'])?$pos['CaseLinkmanTel']:'';//案件联系人电话
          
            $map['case_source_id']=isset($pos['CaseSource'])?$pos['CaseSource']:'';//案源id
            $map['case_conflict_id'] =isset($pos['CaseConflictId'])?$pos['CaseConflictId']:'';//利益冲突规则id
			$year=date('Y');
			if(isset($pos['CaseClassId']))
			{
				$map['case_class_id'] = $pos['CaseClassId'];//案件分类
                if($pos['CaseTypeId'] == 1){//诉讼项目下的分类
                    if($pos['CaseClassId'] == 1 || $pos['CaseClassId'] == 4){//民事案件和商经
                        $map['case_num'] = '('.$year.'）琼云联民诉字第'.$this->Num().'号';
                    }
                    if($pos['CaseClassId'] == 2){//行政
                        $map['case_num'] = '('.$year.'）琼云联行诉字第'.$this->Num().'号';
                    }
                    if($pos['CaseClassId'] == 3){//刑法
                        $map['case_num'] = '('.$year.'）琼云联刑诉字第'.$this->Num().'号';
                    }
                }
				if($pos['CaseTypeId'] == 2){//专项
					$map['case_num'] = '('.$year.'）琼云联专字第'.$this->Num().'号';//案号
				}
                if($pos['CaseTypeId'] == 3){//法律顾问
                    $map['case_num'] = '('.$year.'）琼云联顾字第'.$this->Num().'号';//案号
                }
			}
            $map['case_creater_id'] = $pos['UserId'];//案件创建人ID
            //$map['case_action_second_id']=isset($pos['CaseActionSecondId'])?$pos['CaseActionSecondId']:'';//案件创建人ID
            //$map['case_action_type_id']=isset($pos['CaseActionTypeId'])?$pos['CaseActionTypeId']:'';//案由类型id
            //$map['case_action_id'] = isset($pos['CaseActionId'])?$pos['CaseActionId']:'';//案由id
            //$map['case_action_detail_id'] = isset($pos['CaseActionDetailId'])?$pos['CaseActionDetailId']:'';//案由内容id
            $map['case_create_time'] = time();//案件创建时间
            $map['case_charge_id'] = $pos['CaseChargeId'];//收费方式
            $map['case_subject_matter'] = $pos['CaseSubjectMatter'];//诉讼标的
            $map['case_pay_count'] = $pos['CasePayCount'];//收费数额
            $map['case_stage'] = $pos['CaseStage'];//诉讼阶段
            $map['case_acceptance_id'] = $pos['CaseAcceptanceId'];//受理机关
            $map['case_situation'] = $pos['CaseSituation'];//案件详情
            $map['case_from_id'] = isset($pos['CaseFromId'])?$pos['CaseFromId']:'';//案源律师
            $map['case_member_id'] = isset($pos['CaseMemberId'])?$pos['CaseMemberId']:'';//其他律师id集合
            $map['case_executor_id'] = isset($pos['CaseExecutorId'])?$pos['CaseExecutorId']:'';//项目主办律师id
			if(isset($pos['CaseId']))
			{
				$UpdateCase=DB::connection('mysql_other')->table('yl_case')->where('case_id',$pos['CaseId'])->update($map);
			}
			else
			{
				$InsertId =DB::connection('mysql_other')->table('yl_case')->insertGetId($map);
			}
			//print_r($InsertId);die;
            if(isset($InsertId)){//案件case关联mission将type、action、class对应相等时添加到mission—model表中
				$arr=array();
				//$MaxId = DB::connection('mysql_other')->table('yl_case')->max('case_id');//查询当前添加的案件id
				//print_r($MaxId);die;
				$detil = DB::connection('mysql_other')->table('yl_case_action_detail')->where('case_action_detail_id',$pos['CaseActionDetailId'])->first();
				//print_r($detil);die;
				if($detil){
					$arr['mission_content'] = $detil->case_action_detail_content;//详情内容放入任务文本
					$arr['mission_mode_id'] = $pos['CaseActionDetailId'];//详情内容放入任务文本
					$task = json_decode($detil->case_action_detail_content);
					$total = !empty($task)?count($task):0;//任务总数
					$arr['mission_count']=$total;//任务总数
				}
				$arr['case_id']=$InsertId;
				$arr['mission_complete']=0;//任务完成数
                $MissionAdd = DB::connection('mysql_other')->table('yl_mission')->insert($arr);
				if(isset($pos['LitigationArr'])){	
					$t = array();
					$ti=array();
					foreach ($pos['LitigationArr'] as $k => $v) {//将传过来的当事人信息的数组依次添加依次添加进当事人表中
						$Litigant['litigation_status_id'] = $v['LitigationStatusId'];
						$Litigant['litigant_name'] = $v['LitigantName'];
						//$Litigant['litigant_mobile'] = $v['LitigantMobile'];
						$Litigant['litigant_id_card'] = $v['LitigantIdCard'];
						$Litigant['lawsuit_id'] = isset($v['LitigantLawsuit'])?$v['LitigantLawsuit']:'';
						$Litigant['litigant_type_id'] = isset($v['LitigantTypeId'])?$v['LitigantTypeId']:'';
						$Litigant['litigant_able'] = 1;//项目在办中
						$Litigant['case_id'] = $InsertId;
						$InsterLitigant =DB::connection('mysql_other')->table('yl_litigants')->insert($Litigant); //添加当事人
						if($v['LitigationStatusId'] == 1 ){
                            $t[]= isset($v['LitigantName'])?$v['LitigantName']:'';
                        }
                        if($v['LitigationStatusId'] == 2){
                            $ti[]= isset($v['LitigantName'])?$v['LitigantName']:'';
                        }
					}//查找到当前添加的case中的id添加进当事人表中
					
				}
				 $uptitle =array();
				 $t = implode('',$t);
				 $ti = implode('',$ti);
				 //$uptitle['case_title'] = isset($t)?$t:''.isset($ti)?$ti:''.isset($pos['CaseActionInfo'])?$pos['CaseActionInfo']:'';
				 $uptitle['case_title'] = $t.$ti.$pos['CaseActionInfo'];
				 $UpCaseName = DB::connection('mysql_other')->table('yl_case')->where('case_id',$InsertId)->update($uptitle);
				// print_r($uptitle['case_title']);die;
                if($InsterLitigant){//将传来的当事人id转换为字符串编辑进case中
                    $LitigantId = DB::connection('mysql_other')->table('yl_litigants')->select('litigant_id')->where('case_id',$InsertId)->get()->map(function ($val){
                                    $val = (array)$val;
                                    return $val;
                                    })
                                    ->toArray();
					//print_r($LitigantId);die;
					foreach($LitigantId as $k1 =>$v1){
						$imLitigantId[$k1] = $v1['litigant_id'];
					}
					$im = implode(",",$imLitigantId);
                    $UpCase = DB::connection('mysql_other')->table('yl_case')->where('case_id',$InsertId)->update(['litigants_id'=>$im]);
                }
                if($UpCase){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
                return RequestBackController::Structure(601);
            }elseif(isset($UpdateCase))
			{
				return RequestBackController::Structure(601);
			}else{
                return RequestBackController::Structure(602);
            }
        }

    }
	/*
        结算方式列表
    */
    public function CaseChargeList($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $row = DB::connection('mysql_other')->table('yl_case_charge')->select('case_charge_id as CaseChargeId','case_charge_title as CaseChargeTitle')
                ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })->toArray();
        if($row){
            return RequestBackController::Structure(501,$row);
        }else{
            return RequestBackController::Structure(504);
        }
    }
    /*
    受理机关
    */
    public function CaseAcceptanceList($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $row = DB::connection('mysql_other')->table('yl_case_acceptance')
                ->select('case_acceptance_id as CaseAcceptanceId','case_acceptance_name as CaseAcceptanceName','case_acceptance_flow as CaseAcceptanceFlow','case_acceptance_executor as CaseAcceptanceExecutor','case_acceptance_contact as CaseAcceptanceContact')
                ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })->toArray();
        if($row){
            return RequestBackController::Structure(501,$row);
        }else{
            return RequestBackController::Structure(504);
        }
    }
 /*
    查询单个机关详情
    */
    public function ShowCaseAcceptanceById($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseAcceptanceId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $row = DB::connection('mysql_other')->table('yl_case_acceptance')->select('case_acceptance_id as CaseAcceptanceId','case_acceptance_name as CaseAcceptanceName','case_acceptance_flow as CaseAcceptanceFlow','case_acceptance_executor as CaseAcceptanceExecutor','case_acceptance_contact as CaseAcceptanceContact')
                        ->where('case_acceptance_id',$pos['CaseAcceptanceId'])
                        ->first();
                 if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }

        }
       
    }
	 /*
    新增受理机关
    */
    public function AddCaseAcceptance($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseAcceptanceName','CaseAcceptanceFlow','CaseAcceptanceExecutor','CaseAcceptanceContact'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $add=array();
                $add['case_acceptance_name'] =$pos['CaseAcceptanceName'];
                $add['case_acceptance_flow'] =$pos['CaseAcceptanceFlow'];
                $add['case_acceptance_executor'] =$pos['CaseAcceptanceExecutor'];
                $add['case_acceptance_contact'] =$pos['CaseAcceptanceContact'];
                $row = DB::connection('mysql_other')->table('yl_case_acceptance')->insert($add);
                 if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }

        }
       
    }
	 /*
    编辑单个机关详情
    */
    public function EditCaseAcceptanceById($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseAcceptanceId','CaseAcceptanceName','CaseAcceptanceFlow','CaseAcceptanceExecutor','CaseAcceptanceContact'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $map = array();
                $map['case_acceptance_name'] = $pos['CaseAcceptanceName'];
                $map['case_acceptance_flow'] = $pos['CaseAcceptanceFlow'];
                $map['case_acceptance_executor'] = $pos['CaseAcceptanceExecutor'];
                $map['case_acceptance_contact'] = $pos['CaseAcceptanceContact'];
                $row = DB::connection('mysql_other')->table('yl_case_acceptance')
                        ->where('case_acceptance_id',$pos['CaseAcceptanceId'])
                        ->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }

        }
       
    }
	/*
    删除机关单个信息
    */
    public function DeleteCaseAcceptanceById($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','CaseAcceptanceId'])){//
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $row = DB::connection('mysql_other')->table('yl_case_acceptance')->where('case_acceptance_id',$pos['CaseAcceptanceId'])->first();
                if($row){
                    $DeRow = DB::connection('mysql_other')->table('yl_case_acceptance')->where('case_acceptance_id',$pos['CaseAcceptanceId'])->delete();
                    if($DeRow){
                        return RequestBackController::Structure(601);
                    }else{
                        return RequestBackController::Structure(602);
                    }
                }else{
                    return RequestBackController::Structure(505);
                }

            }else{
                return RequestBackController::Structure(403);
            }

        }
       
    }

    /*
    首页用户统计页面
    */
    public function IndexCount($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','page','pageSize'])){//
            return RequestBackController::Structure(502);
        }else{
            //我的任务所有案例：
            $page = isset($pos['page']) ? $pos['page']  : 1;
            $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
            $offset = ($page - 1) * $pageSize;
            $CaseInfo = DB::connection('mysql_other')->table('yl_case as c')->select('c.case_id as CaseId','c.case_title as CaseTitle','c.case_create_time as CaseCreateTime','c.case_executor_id as CaseExecutorId','u.user_id as UserId','u.user_name as UserName','u.user_img as UserImg','m.case_id as CaseId','m.mission_content as MissionContent','m.mission_count as MissionCount','m.mission_complete as MissionComplete','m.mission_end as MissionEnd')->join("yl_user as u",function($join){
                                                    $join->on('c.case_executor_id','=','u.user_id');
                                                })
                                             ->join("yl_mission as m",function($join){
                                                    $join->on('c.case_id','=','m.case_id');
                                                })
                    ->where('case_member_id','like','%'.$pos['UserId'].'%')->orWhere('case_from_id','like','%'.$pos['UserId'].'%')->orWhere('case_executor_id','like','%'.$pos['UserId'].'%');
                    $count =$CaseInfo->count();
                    $CaseInfo=$CaseInfo->offset($offset)->limit($pageSize)
                    ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['CaseId'];
                    })
                    ->toArray();
                    $sum_page = ceil($count/$pageSize);  //总页码
                    if($pos['page'] > $sum_page && $sum_page > 0 ){
                        return RequestBackController::Structure(301);
                    }else{
                        if($CaseInfo){
                            $result = $this->ListData($CaseInfo); 
                            return RequestBackController::Structure(501,$result);
                        }else{
                            return RequestBackController::Structure(504);
                        }
                            
                    }
        }
    }
    /*
    案件任务展示(显示mission表中mission_content字段内容)
    */
    public function ShowTask($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId'])){//案件id
            return RequestBackController::Structure(502);
        }else{
            $TaskInfo = DB::connection('mysql_other')->table('yl_mission')->where('case_id', '=', $pos['CaseId'])->get()->toArray();
            //var_dump($TaskInfo);die();
            if($TaskInfo){
                return RequestBackController::Structure(501,$TaskInfo);
            }else{
                return RequestBackController::Structure(504);
            }
        }

    }


    /**
    单个案件审批approval展示(分页、模糊查询(项目名称、主体名称、客户名称==？？申请人名称))
    */
    public function CaseApproval($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['page','pageSize','CaseId'])){
            return RequestBackController::Structure(502);
        }else{  
            $page = isset($pos['page']) ? $pos['page']  : 1;
            $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
            $offset = ($page - 1) * $pageSize;
            $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                                            ->join("yl_user as u",function($join){
                                                    $join->on('a.approval_creater_id','=','u.user_id');
                                                })
                                            ->join("yl_case as c",function($join){
                                                    $join->on('c.case_id','=','a.case_id');
                                                })
                                            ->join("yl_approval_mode as m",function($join){
                                                    $join->on('a.approval_mode_id','=','m.approval_mode_id');
                                                })
                                            ->join("yl_approval_results as r",function($join){
                                                    $join->on('a.approval_results_id','=','r.approval_results_id');
                                                })
                                            ->where('c.case_id','=',$pos['CaseId']);
                                            $count =$ApprovalInfo->count();
                                            $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                    ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['CaseId'];
                    })
                    ->toArray();
                   //var_dump($ApprovalInfo);die();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
                            $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                        }
						$ApprovalInfos['List'] = $ApprovalInfo;
                        $ApprovalInfos['count'] = $count;
                        return RequestBackController::Structure(501,$ApprovalInfos);
                    }else{
                        return RequestBackController::Structure(504);
                    }
                            
                }
       
        }

    }


    /**
    所有案件审批approval展示(分页、模糊查询(项目名称、主体名称、客户名称==？？申请人名称))temp=>是否为我发起的我审批的我抄送的
    */
    public function ShowApproval($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['page','pageSize','temp','UserId'])){
            return RequestBackController::Structure(502);
        }else{  
           // var_dump($pos['UserId']);
            $page = isset($pos['page']) ? $pos['page']  : 1;
            $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
            $offset = ($page - 1) * $pageSize;
            $map = array();
            if($pos['temp'] == 1){//我抄送的（未完成）
                $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                                            ->join("yl_user as u",function($join){
                                                    $join->on('a.approval_creater_id','=','u.user_id');
                                                })
                                            ->join("yl_case as c",function($join){
                                                    $join->on('c.case_id','=','a.case_id');
                                                })
                                            ->join("yl_approval_mode as m",function($join){
                                                    $join->on('a.approval_mode_id','=','m.approval_mode_id');
                                                })
                                            ->join("yl_approval_results as r",function($join){
                                                    $join->on('a.approval_results_id','=','r.approval_results_id');
                                                })
                                            //->Where('m.approval_copy_id','like','%'.$pos['UserId'].'%');
											->whereRaw("FIND_IN_SET($pos[UserId],m.approval_copy_id)");
                                            $count =$ApprovalInfo->count();
                                            $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                    // ->toSql();
                    // print_r($ApprovalInfo);exit();
                    // $ApprovalInfo
                    ->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
                    //var_dump($ApprovalInfo);die();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
                            $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                            //$ApprovalInfo['count'] = $count;
                        }
						$ApprovalInfos['List'] = $ApprovalInfo;
                        $ApprovalInfos['count'] = $count;
                        return RequestBackController::Structure(501,$ApprovalInfos);
                    }else{
                        return RequestBackController::Structure(504);
                    }
                            
                }
            }if($pos['temp'] == 2){//我审批的
                //$map['m.approval_assessor_id']=['like','%'.$pos['UserId'].'%'];
                $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                                            ->join("yl_user as u",function($join){
                                                    $join->on('a.approval_creater_id','=','u.user_id');
                                                })
                                            ->join("yl_case as c",function($join){
                                                    $join->on('c.case_id','=','a.case_id');
                                                })
                                            ->join("yl_approval_mode as m",function($join){
                                                    $join->on('a.approval_mode_id','=','m.approval_mode_id');
                                                })
                                            ->join("yl_approval_results as r",function($join){
                                                    $join->on('a.approval_results_id','=','r.approval_results_id');
                                                })
                                            //->where('m.approval_assessor_id','like','%'.$pos['UserId'].'%');
											->whereRaw("FIND_IN_SET($pos[UserId],m.approval_assessor_id)");
                                            $count =$ApprovalInfo->count();
                                            $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                    ->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['CaseId'];
                    })
                    ->toArray();
                    //var_dump($ApprovalInfo);die();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
                            $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                           // $ApprovalInfo['count'] = $count;
                        }
						$ApprovalInfos['List'] = $ApprovalInfo;
                        $ApprovalInfos['count'] = $count;
                        return RequestBackController::Structure(501,$ApprovalInfos);
                    }else{
                        return RequestBackController::Structure(504);
                    }
                            
                }

            }if($pos['temp'] == 3){//我发起的
                //$map['m.approval_mode_creater']=['like','%'.$pos['UserId'].'%'];
                $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                                            ->join("yl_user as u",function($join){
                                                    $join->on('a.approval_creater_id','=','u.user_id');
                                                })
                                            ->join("yl_case as c",function($join){
                                                    $join->on('c.case_id','=','a.case_id');
                                                })
                                            ->join("yl_approval_mode as m",function($join){
                                                    $join->on('a.approval_mode_id','=','m.approval_mode_id');
                                                })
                                            ->join("yl_approval_results as r",function($join){
                                                    $join->on('a.approval_results_id','=','r.approval_results_id');
                                                })
                                            //->where('m.approval_mode_creater','like','%'.$pos['UserId'].'%');
											 ->whereRaw("FIND_IN_SET($pos[UserId],m.approval_mode_creater)");
                                            $count =$ApprovalInfo->count();
                                            $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                    ->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['CaseId'];
                    })
                    ->toArray();
                    //var_dump($ApprovalInfo);die();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
                            $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                            //$ApprovalInfo['count'] = $count;
                        }
						$ApprovalInfos['List'] = $ApprovalInfo;
                        $ApprovalInfos['count'] = $count;
                        return RequestBackController::Structure(501,$ApprovalInfos);
                    }else{
                        return RequestBackController::Structure(504);
                    }
                            
                }
            }else{//显示全部
                $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                                            ->join("yl_user as u",function($join){
                                                    $join->on('a.approval_creater_id','=','u.user_id');
                                                })
                                            ->join("yl_case as c",function($join){
                                                    $join->on('c.case_id','=','a.case_id');
                                                })
                                            ->join("yl_approval_mode as m",function($join){
                                                    $join->on('a.approval_mode_id','=','m.approval_mode_id');
                                                })
                                            ->join("yl_approval_results as r",function($join){
                                                    $join->on('a.approval_results_id','=','r.approval_results_id');
                                                });
                                            $count =$ApprovalInfo->count();
                                            $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                                            ->get()
                                            ->map(function ($val){
                                                $val = (array)$val;
                                                return $val;
                                            })
                                            ->keyBy(function($item)
                                            {
                                                return $item['CaseId'];
                                            })
                                            ->toArray();
                    //var_dump($ApprovalInfo);die();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
                            $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                            //$ApprovalInfo['count'] = $count;
                        }
						$ApprovalInfos['List'] = $ApprovalInfo;
                        $ApprovalInfos['count'] = $count;
                        return RequestBackController::Structure(501,$ApprovalInfos);
                    }else{
                        return RequestBackController::Structure(504);
                    }
                            
                }


            }


       
        }

    }

    /*
    委托人和诉讼地位列表
    */
        
    public function ShowIdentity($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();

        $LitigationStatusName = DB::connection('mysql_other')->table('yl_litigation_status')->select('litigation_status_id as litigationStatusId','litigation_status_name as LitigationStatusName','litigation_conflict_id as LitigationConflictId')->where('litigation_status_id','>',0)->get()->toArray();
        $LitigantTypeTitle = DB::connection('mysql_other')->table('yl_litigant_type')->select('litigant_type_id as LitigantTypeId','litigant_type_title as LitigantTypeTitle','litigant_type_nature as LitigantTypeNature')->get()->toArray();
		$row = DB::connection('mysql_other')->table('yl_litigation_lawsuit')->select('litigation_lawsuit_id as LitigationLawsuitId','litigation_lawsuit_name as LitigationLawsuitName')->get()->toArray();
		
        if($LitigationStatusName && $LitigantTypeTitle){
            $type['LitigationStatusName'] = $LitigationStatusName;
            $type['LitigantTypeTitle'] =$LitigantTypeTitle;
			$type['LitigationLawsuit']=$row;
            return RequestBackController::Structure(501,$type);
        }else{
            return RequestBackController::Structure(504);
        }
    }
		/*
	所有律师列表
	*/
	public function LawList($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['key'])){//模糊查询
            return RequestBackController::Structure(502);
        }else{
            $LawName = DB::connection('mysql_other')->table('yl_user')->select('user_id as UserId','user_name as UserName','user_img as UserImg')
            ->Where('user_name','like','%'.$pos['key'].'%')
            ->get()->toArray();

            if($LawName){
                return RequestBackController::Structure(501,$LawName);
            }else{
                return RequestBackController::Structure(504);
            }
        }
        
    }





   
}